<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\adminapi\controller\v1\car;


use app\adminapi\controller\AuthController;
use app\services\car\BaseSendFeeServices;
use think\facade\App;

/**
 * Class BaseSendFee
 * @package app\adminapi\controller\v1\shendeng
 */
class BaseSendFee extends AuthController
{
    public function __construct(App $app,BaseSendFeeServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 列表
     * @return \think\Response
     */
    public function getList(){
        $where = $this->request->getMore([
            ['store_id',''],
        ]);
        $admin = $this->request->adminInfo();
        if($admin['store_id']) $where['store_id'] = $admin['store_id'];
        return app('json')->success($this->services->getList($where,true));
    }

    /**
     * 添加/修改
     * @return mixed
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(){
        $data = $this->request->getMore([
            ['id', 0],
        ]);
        $admin = $this->request->adminInfo();
        return app('json')->success($this->services->add((int)$data['id'],$admin));
    }

    /**
     * 保存
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(){
        $data = $this->request->postMore([
            ['id', 0],
            ['store_id', ''],
            ['start_time', ''],
            ['end_time', ''],
            ['mink_km',''],
            ['max_km', ''],
            ['send_fee', ''],
            ['before_min', ''],
        ]);
        if (!$data['store_id']) return app('json')->fail('请选择门店');
        if (!$data['start_time']) return app('json')->fail('请选择服务开始时间');
        if (!$data['end_time']) return app('json')->fail('请选择服务结束时间');
//        if (!$data['mink_km']) return app('json')->fail('请输入最小公里数');
        if (!$data['max_km']) return app('json')->fail('请输入大公里数');
        if (!$data['send_fee']) return app('json')->fail('请输入服务费用');
        if (!$data['before_min']) return app('json')->fail('请选择提前预约时间');
        $this->services->save((int)$data['id'], $data);
        return app('json')->success('提交成功');
    }

    /**
     * 删除
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(){
        $data = $this->request->getMore([
            ['id', 0],
        ]);
        if (!$data['id']) return app('json')->fail('参数错误');
        return app('json')->success($this->services->delSendFee((int)$data['id']));
    }
}
